layui.config({
    base: "js/"
}).use(['form', 'layer', 'jquery', 'laypage','laytpl','util'], function () {
    var form = layui.form
        , layer = parent.layer === undefined ? layui.layer : parent.layer
        , laypage = layui.laypage,laytpl = layui.laytpl, $ = layui.jquery;

    dataList(1);

    /**
     * 查询
     */
    form.on('submit(queryForm)', function(data){
        dataList(1);
        return false;
    });

    /**
     * 查询数据列表
     * @param number
     */
    function dataList(number) {
        var options = {
            dataType: "json",
            success: function (data) {
                if(data && data.error) {
                    layer.msg(data.error.msg);
                    return;
                }
                var listData = {"list":data.page.records};
                var getTpl = dataListTpl.innerHTML,view = document.getElementById('listDataBody');

                laytpl(getTpl).render(listData, function(html){
                    view.innerHTML = html;
                });
                form.render();
                if(number === 1) {
                    //分页标签
                    pageBar(data.page.total,data.page.size);
                }
            },
            error: function (e) {
                layer.msg(JSON.stringify(e));
            }
        };
        $("#listForm").ajaxSubmit(options);
    }

    /**
     * 数据分页
     * @param count
     * @param limit
     */
    function pageBar(count,limit) {
        laypage.render({
            elem: "page",
            limit: limit,
            count: count,
            theme: '#1E9FFF',
            layout: ['count', 'prev', 'page', 'next'],
            jump: function(obj, first){
                if(!first){
                    $("#number").val(obj.curr);
                    dataList(obj.curr);
                }
            }
        });
    }

    /**
     * 添加博客类型
     */
    $("body").on("click", ".blogtypeAdd_btn", function () {
        addOrEdit("添加博客类型", "/m/blogtype/add", "添加", false);
    })

    /**
     * 编辑博客类型
     */
    $("body").on("click", ".blogtypeEdit", function () {
        var id = $(this).data("id");
        addOrEdit("编辑博客类型", "/m/blogtype/update/" + id, "编辑", true, "/m/blogtype/" + id);
    })

    /**
     * 冻结博客类型
     */
    form.on('switch(isShow)', function (obj) {
        var id = obj.elem.getAttribute("data-id");
        var status = 0;
        if (obj.elem.checked) {
            status = 1;
        }
        obj.elem.disabled=true;
        post("/m/blogtype/frozen/" + id, {
            "status": status
        },function (data) {
            if (data.code == 0) {
                obj.elem.disabled=false;
                layer.msg(data.msg, {icon: 6,time:500});
                if(status==1){
                    obj.othis.parent().prev().text("隐藏").css({"color":"#35A7FE"})
                }else{
                    obj.othis.parent().prev().text("显示").css({"color":"#666"});
                }

            }
        });
    })

    /**
     * 删除博客类型
     */
    $("body").on("click", ".blogtypeDel", function () {
        var _this = $(this);
        layer.confirm('确定删除此信息？', {
            icon: 3,
            title: '提示信息'
        }, function (index) {

            var id = _this.attr("data-id");
            post("/m/blogtype/delete/" + id, {}, function (data) {
                layer.msg(data.msg||"操作成功~", {icon: 6});
                _this.parents("tr").remove();
                layer.close(index);
            }, false, false);

        });
    })

    /**
     * 添加或编辑博客类型
     * @param title
     * @param postUrl
     * @param yesBtnName
     * @param isEdit
     * @param queryUrl
     */
    function addOrEdit(title, postUrl, yesBtnName, isEdit, queryUrl) {
        var index = layui.layer.open({
            title: title,
            anim: 2,
            content: $("#addblogtype").html(),
            btn: [yesBtnName, '取消'],
            yes: function () {
                var blogtypeName = $(".blogtypeName").val(),blogtypeIcon=$(".blogtypeIcon").val();
                if (!blogtypeName) {
                    layer.msg("请填写博客类型名称！", {
                        icon: 5
                    });
                    return;
                }
                post(postUrl, {
                    "name": blogtypeName,
                    "icon":blogtypeIcon
                },function (data) {
                    if(data.code==0){
                        layui.layer.close(index);
                        layer.msg(data.msg||"操作成功~", {icon: 6});
                        dataList(1);
                    }
                })

            },
            success: function () {
                if (isEdit) {
                    get(queryUrl,null,function (data) {
                        if(data.code==0){
                            $(".blogtypeName").val(data.blogtype.name);
                            $(".blogtypeIcon").val(data.blogtype.icon);
                        }
                    })
                }
            }
        })
    }
})
